﻿using System.Collections.Generic;
using System.Linq;
using eCommMongo.Core.Data;
using eCommMongo.Core.Domain.Customers;
using eCommMongo.Core.Domain.FriendShips;

namespace eCommMongo.Service.FriendShips
{
    public class FriendShipService:IFriendShipService
    {

        private IMongoRepository<FriendShip> _friendShipRep;
        private IMongoRepository<Customer> _customerRep;

        public FriendShipService(IMongoRepository<FriendShip> friendShipRep, IMongoRepository<Customer> customerRep)
        {
            _friendShipRep = friendShipRep;
            _customerRep = customerRep;
        }

        public void Detroy(string ownerId, string friendId)
        {
            throw new System.NotImplementedException();
        }

        public IList<Customer> Follows(string customerId)
        {
            throw new System.NotImplementedException();
        }

        public IList<Customer> Follows(int customerId)
        {
            //var query = from r in _friendShipRep.Table
            //            join cust in _customerRep.Table 
            //            on r.FriendId equals  cust.Id
            //            where r.OwnerId== customerId
            //            select cust;

            //var result = query.Distinct().ToList();

            //if (result != null)
            //{
            //    return CustomerMapper.ToCustomers(result);
            //}
            return null;
        }

        public IList<Customer> Fans(string customerId)
        {
            //var query = from r in _friendShipRep.Table
            //            join cust in _customerRep.Table
            //            on r.OwnerId equals cust.Id
            //            where r.FriendId==customerId 
            //            select cust;

            //var result = query.Distinct().ToList();

            //if (result != null)
            //{
            //    return CustomerMapper.ToCustomers(result);
            //}
            return null;
        }



        public void CreateFriendShip(string ownerId, string toId)
        {
            var friendship = new FriendShip()
                {
                    Owner = new Customer(){Id = ownerId},
                    Friend = new Customer(){Id = toId},
                };
            _friendShipRep.Insert(friendship);
        }

        public bool IsFriend(string currentCustomerId, int customerId)
        {
            throw new System.NotImplementedException();
        }

        public bool IsFriend(string ownerId, string friendId)
        {
            var query = from f in _friendShipRep.Table()
                        where f.Owner.Id == ownerId
                              && f.Friend.Id == friendId
                        select f;
            return query.Any();

        }
    }
}